package com.erhuogo.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.erhuogo.dao.MessageDao;
import com.erhuogo.entity.Message;

/**
 * 留言Dao的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:45:19
 */
public class MessageDaoImpl extends HibernateDaoSupport implements MessageDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(MessageDaoImpl.class);

	// 1.分页条件查询留言个数Dao实现类
	@Override
	public int countByParams(String id, String commodityIdStr, String writerIdStr, String readerIdStr, String startDate,
			String endDate) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(Message.class);
		if(!StringUtils.isEmpty(id)){
			Integer messageId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(messageId));
		}
		if(!StringUtils.isEmpty(commodityIdStr)){
			Integer commodityId = Integer.valueOf(commodityIdStr);
			criteria.add(Restrictions.eq("commodityId", commodityId));
		}
		if(!StringUtils.isEmpty(writerIdStr)){
			Integer writerId = Integer.valueOf(writerIdStr);
			criteria.add(Restrictions.eq("writerId", writerId));
		}
		if(!StringUtils.isEmpty(readerIdStr)){
			Integer readerId = Integer.valueOf(readerIdStr);
			criteria.add(Restrictions.eq("readerId", readerId));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("time", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<Message> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的留言信息");
		} else{
			logger.info("没有找到符合查询条件求的留言信息");
		}
		return count;
	}

	// 2.分页条件查询留言集合Dao实现类
	@Override
	public List<Message> findPageByParams(String id, String commodityIdStr, String writerIdStr, String readerIdStr,
			String startDate, String endDate) {
		List<Message> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(Message.class);
		if(!StringUtils.isEmpty(id)){
			Integer messageId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(messageId));
		}
		if(!StringUtils.isEmpty(commodityIdStr)){
			Integer commodityId = Integer.valueOf(commodityIdStr);
			criteria.add(Restrictions.eq("commodityId", commodityId));
		}
		if(!StringUtils.isEmpty(writerIdStr)){
			Integer writerId = Integer.valueOf(writerIdStr);
			criteria.add(Restrictions.eq("writerId", writerId));
		}
		if(!StringUtils.isEmpty(readerIdStr)){
			Integer readerId = Integer.valueOf(readerIdStr);
			criteria.add(Restrictions.eq("readerId", readerId));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("time", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<Message> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的留言信息");
		} else{
			logger.info("没有找到符合查询条件求的留言信息");
		}
		return result;
	}

	// 4.根据id删除留言Dao实现类
	@Override
	public void deleteById(String id) throws Exception{
		if(StringUtils.isEmpty(id)){
			throw new Exception();
		}
		Integer messageId = Integer.valueOf(id);
		String hql = "from Message where id = ?";
		List<Message> list = this.getHibernateTemplate().find(hql, messageId);
		if(list!=null && list.size()>0){
			Message message = list.get(0);
			this.getHibernateTemplate().delete(message);
		}
	}
}
